package com.example.casper.Experiment2024;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;


import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    //验证字符串资源
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.example.casper.Experiment2024", appContext.getPackageName());
    }
    //验证颜色资源
    @Test
    public void testColorResource() {
        // 获取应用程序上下文
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        // 获取颜色资源
        int color = appContext.getResources().getColor(R.color.purple_200, null);
        // 验证颜色资源是否正确（可以通过颜色的 RGB 值进行比较）
        assertEquals(Color.parseColor("#FFBB86FC"), color);
    }
    //验证图片资源
    @Test
    public void testDrawableResource() {
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        // 获取图片资源
        Drawable drawable = appContext.getDrawable(R.drawable.foot);
        // 验证图片资源是否正确加载
        assertNotNull(drawable);
    }
    //测试本地存储
    @Test
    public void testSharedPreferences() {
        // 获取应用程序上下文
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        // 获取 SharedPreferences
        SharedPreferences sharedPreferences = appContext.getSharedPreferences("test_prefs", Context.MODE_PRIVATE);
        // 保存数据
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username", "test_user");
        editor.apply();

        // 验证数据是否已保存
        String storedUsername = sharedPreferences.getString("username", "");
        assertEquals("test_user", storedUsername);
    }
    //测试文件读写
    @Test
    public void testFileStorage() throws IOException {
        // 获取应用程序上下文
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        // 获取文件输出流
        FileOutputStream fos = appContext.openFileOutput("test_file.txt", Context.MODE_PRIVATE);
        String content = "Hello, World!";
        fos.write(content.getBytes());
        fos.close();

        // 获取文件输入流
        FileInputStream fis = appContext.openFileInput("test_file.txt");
        byte[] buffer = new byte[content.length()];
        fis.read(buffer);
        fis.close();

        // 验证文件内容是否正确
        assertEquals(content, new String(buffer));
    }

}